﻿
namespace storge
{
	using System;
	using System.Collections.Generic;
	using System.IO;

	public class PhysicalDriveFactory : IDriveFactory
	{
		public IEnumerable<IDrive> ListAllDrives()
		{
			var allDrives = new List<IDrive>();
			
			foreach (DriveInfo foundDrive in DriveInfo.GetDrives())
			{
				try
				{
					allDrives.Add(new PhysicalDrive(foundDrive.Name, foundDrive.VolumeLabel));
				}
				catch (System.IO.IOException)
				{
					// may be trying to connect to drives that are not ready - swallow this exception
				}
			}

			return allDrives;
		}
		
		public IDrive  FindDrive(Predicate<IDrive> match)
		{
			IDrive specificDrive = null;
			
			foreach (DriveInfo foundDrive in DriveInfo.GetDrives())
			{
				try
				{
					var candidate = new PhysicalDrive(foundDrive.Name, foundDrive.VolumeLabel);

					if (match != null && match(candidate))
					{
						specificDrive = candidate;
						break;
					}
				}
				catch (System.IO.IOException)
				{
					// may be trying to connect to drives that are not ready - swallow this exception
				}
			}

			return specificDrive;
		}
	}
}
